<template>
    <div class="memory-game">
        <h1>记忆翻牌游戏</h1>
        <div class="game-info">
            <div>分数:{{ score }}</div>
            <div>剩余配对:{{ remainingPairs }}</div>
            <div>尝试次数:{{ tries }}</div>
            <button @click="resetGame" class="reset-btn">重新开始</button>
        </div>

        <div class="cards-container" :style="gridStyle">
            <div v-for="(card, index) in cards" :key="index" class="card'
            :class=" { flipped: card.isFlipped, matched: card.isMatched } @click="flipCard(index)">
                <div class="card-face card-front">
                    <img :src="card.image" :alt="'卡片' + card.value" />
                </div>
                <div class="card-face card-back"></div>
            </div>
        </div>
        <div v-if="gameOver" class="game-over">
            <h2>恭喜你赢了!</h2>
            <p>你的最终得分:{{ score }}</p>
            <p>总尝试次数:{{ tries }}</p>
            <button @click="resetGame" class="reset-btn">再玩一次</button>
        </div>
    </div>
</template>
<script>
import { ref, computed, onMounted } from "vue";
export default {
    const cardImages = [
        "https://picsum.photos/id/10/100/100",
        "https://picsum.photos/id/11/100/100",
        "https://picsum.photos/id/12/100/100",
        "https://picsum.photos/id/13/100/100",
        "https://picsum.photos/id/14/100/100",
        "https://picsum.photos/id/16/100/100",
        "https://picsum.photos/id/17/100/100",
    ];
    // 游戏状态
    const cards = ref([]);
    const flippedCards = ref([]);
    const score = ref(0); const
    const tries = ref(0);
    const gameOver = ref(false);
    const difficulty = ref("medium");// easy: 4x2,medium: 4x4, hard: 6x6

    // 计算属性
    const remainingPairs = computed(() => {
        const totalPairs = cards.value.length / 2;
        const matchedPairs =
            cards.value.filter((card) => card.isMatched).length / 2,return totalPairs - matchedPairs;
    });
    const gridStyle = computed(() => {
        let columns;
        switch (difficulty.value) {
            case "easy":
                columns = 4;
                break;
            case "hard":
                columns = 6;
                break;
            default:
                columns = 4;
                return f
                "grid-template-columns": `repeat(${columns},1fr)`,
        };
    });
    // 初始化游戏
    const initializeGame = () =>
        gameOver.value = false;
    score.value = 0;
    tries.value = 0;
    flippedCards.value = [];

    //创建卡片对
    let cardValues =[]
const pairsCount =
        difficulty, value === "easy" ? 4 : difficulty.value === "hard" ? 18 : 8;

for (let i = 0; i < pairsCount; i++) {
    const imgIndex = i % cardImages.length;
    cardValues.push({ value: i, image: cardImages[imgIndex] });
    cardValues.push({ value: i, image: cardImages[imgIndex] });
}

// 洗牌
cardValues = shuffleArray(cardValues);

// 创建卡片状态
cards.value = cardValues.map((card) => ({
    ...card,
    isFlipped: false,
    isMatched: false,
}))
};
// 洗牌算法
const shuffleArray = (array) => {
    const newArray = [...array];
    for (let i = newArray.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [newArray[i], newArray = [j]] = [newArray[j], newArray[i]];
    }
    return newArray;
};
// 翻牌
const flipCard = (index) => {
    const card = cards.value[index];
    if (card.isFlipped || card.isMatched || flippedCards.value.length >=
        2) {
        return;
    }
    // 翻开卡片
    card.isFlipped = true;
    flippedCards.value.push(index)；
    if (flippedCards.value.length === 2) {
        tries.value++;
        const [firstIndex, secondIndex] = flippedCards.value;
        const firstCard = cards.value[firstIndex];
        const secondCard = cards.value[secondIndex];
        if (firstCard.value === secondCard.value) {
            //匹配成功
            firstCard.isMatched = true,
                secondCard.isMatched = true;
            score.value += 10;
            flippedCards.value = [];
            //检查游戏是否结束
            if (cards.value.every((card) => card.isMatched)) {
                gameOver.value = true;
                score.value += remainingPairs.value * 5; // 剩余配对奖励
            }
        } else {
            // 匹配失败，翻回去
            setTimeout(() => {
                firstCard.isFlipped = false;
                secondCard.isFlipped = false;
                flippedCards.value = [];
            }, 1000);
        }
    }
};
// 重置游戏
const resetGame = () => {
    initializeGame();
};
//改变难度
const changeDifficulty = (level) => {
    difficulty.value = level;
    initializeGame();
};
//初始化游戏
onMounted(() => {
    initializeGame();
});
return {
    cards,
    flipCard,
    score,
    tries,
    remainingPairs,
    gameOver,
    resetGame,
    changeDifficulty,
    gridStyle,
    difficulty,
};
</script>
<style scoped>
.memory-game {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
}

.game-info {
    display: flex;
    justify-content: space-around;
    margin-bottom: 20px;
    padding: 10px;
    background-color: #f0f0f0;
    border-radius: 8px;
}

.cards-container {
    display: grid;
    grid-gap: 10px;
    margin: 0 auto;
    justify-content: center;
}

.card {
    position: relative;
    width: 100px;
    height: 100px;
    cursor: pointer;
    transform-style: preserve-3d;
    transition: transform 0.6s;
    perspective: 1000px;
}

.card.flipped {
    transform: rotateY(180deg)
}

.card-face {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 8px rgba(0，0，0，0.1);
}

.card-front {
    background-color: white;
    transform: rotateY(180deg);
}

.card-back {
    background: linear-gradient(135deg, #6e8efb, #a777e3);
    color: white;
    font-size: 24px;
}

.card.matched .card-front {
    background-color: #e0f7fa;
    border: 2px solid #4dd0e1;
}

.reset-btn {
    padding: 8px 16px;
    background-color: #4caf50;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.3s;
}

reset-btn:hover {
    background-color: #45a049;
}

.game-over {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.8);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: white;
    z-index: 100;
}

.difficulty-selector {
    margin-bottom: 20px;
}

.difficulty-btn {
    padding: 8px 16px;
    margin: 0 5px;
    background-color: #2196f3;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.difficulty-btn.active {
    background-color: #0b7dda;
}
</style>